﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using EmoneyWeiXinBLL;
using EmoneyWeiXinCommon;
using EmoneyWeiXinEntity;
using EmoneyWeiXinThreadBLL;
namespace EmoneyWeiXin.Controllers
{
    public class BaseConfigurationController : Controller
    {
        //
        // GET: /BaseConfiguration/

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult WeixinUser()
        {
            return View();
        }

        public ActionResult WeixinUserMemu(int weixinid = 0)
        {
            ViewBag.weixinid = weixinid;
            var uid = 0;
            UserEntity us = LoginUserInfo.GetLoginUserCache();
            if (us != null)
                uid = us.id;
            weixinUserBLL bll = new weixinUserBLL();
            ViewBag.listWeixinMemu = bll.getWeixinUser1(uid);
            return View();
        }

        /// <summary>
        /// 列表数据
        /// </summary>
        /// <param name="pageindex"></param>
        /// <param name="pagecount"></param>
        /// <returns></returns>
        public ActionResult WeiXinUser_Data(int pageindex = 1, int pagecount = 1)
        {
            weixinUserBLL bll = new weixinUserBLL();
            ViewBag.listweixinuser = bll.GetWeixinUser("", pageindex, pagecount);
            return View();
        }

        public string getWeixinOne(string id)
        {
            weixinUserBLL bll = new weixinUserBLL();
            var query = bll.GetWeixinUser(id, 1, 1000);
            if (query != null && query.Count > 0)
            {
                return JsonTools.ToJson(query[0]);
            }
            return "";
        }

        public int weixindel(string id)
        {
            weixinUserBLL bll = new weixinUserBLL();
            int aa = bll.weixindel(id);
            if (aa > 0)
            {
                ThreadWeiXinBLL.getWeixinUser();
            }
            return aa;
        }

        /// <summary>
        /// 条数
        /// </summary>
        /// <param name="tableName"></param>
        /// <returns></returns>
        public int GetPageHtmlCount(string tableName)
        {
            weixinUserBLL bll = new weixinUserBLL();
            return bll.GetTableNameCount(tableName);
        }

        /// <summary>
        /// 更新微信平台用户
        /// </summary>
        /// <returns></returns>
        public int UpdateWeixinUser()
        {
            UserEntity us = LoginUserInfo.GetLoginUserCache();
            if (us == null || string.IsNullOrEmpty(us.Username))
            {
                return -99;
            }
            string title = HttpUtility.UrlDecode(Request.QueryString["title"]);
            string Author = HttpUtility.UrlDecode(Request.QueryString["Author"]);
            string AppId = HttpUtility.UrlDecode(Request.QueryString["AppId"]);
            string AppSecret = HttpUtility.UrlDecode(Request.QueryString["AppSecret"]);
            string Token = HttpUtility.UrlDecode(Request.QueryString["Token"]);
            string EncodingAESKey = HttpUtility.UrlDecode(Request.QueryString["EncodingAESKey"]);
            string ServicesUrl = HttpUtility.UrlDecode(Request.QueryString["ServicesUrl"]);
            string gzUrl = HttpUtility.UrlDecode(Request.QueryString["gzUrl"]);
            int IsMicrocommunity = Convert.ToInt32(HttpUtility.UrlDecode(Request.QueryString["IsMicrocommunity"]));
            string MicrocommunityUrl = HttpUtility.UrlDecode(Request.QueryString["MicrocommunityUrl"]);
            int hidWeixinUserid = Convert.ToInt32(HttpUtility.UrlDecode(Request.QueryString["hidWeixinUserid"]));
            string WeiXinNumber = HttpUtility.UrlDecode(Request.QueryString["WeiXinNumber"]);
            string Remark = HttpUtility.UrlDecode(Request.QueryString["Remark"]);
            weixinUserEntity weixin = new weixinUserEntity();
            weixin.title = title;
            weixin.Author = Author;
            weixin.AppId = AppId;
            weixin.AppSecret = AppSecret;
            weixin.Token = Token;
            weixin.EncodingAESKey = EncodingAESKey;
            weixin.servicesUrl = ServicesUrl;
            weixin.gzUrl = gzUrl;
            weixin.IsMicrocommunity = IsMicrocommunity;
            weixin.MicrocommunityUrl = MicrocommunityUrl;
            weixin.id = hidWeixinUserid;
            weixin.UpdateUser = us.Username;
            weixin.weixinNumber = WeiXinNumber;
            weixin.remark = Remark;
            weixinUserBLL bll = new weixinUserBLL();
            int aa = bll.weixinUpdate(weixin);
            if (aa > 0)
            {
                ThreadWeiXinBLL.getWeixinUser();
            }
            return aa;
        }



        //平台用户管理页面
        public ActionResult UserList()
        {
            ViewBag.leftid = int.Parse(Request["leftid"]);
            return View();
        }
        //显示用户列表
        public string GetUserList()
        {
            var re = "";
            var total = 0;
            var pindex = int.Parse(Request["pindex"]);
            var psize = int.Parse(Request["psize"]);
            UserBLL ubll = new UserBLL();
            var list = ubll.getUserList(pindex, psize, out total);
            if (list != null && list.Count > 0)
            {
                re = JsonTools.ToJson(list);
            }
            return total.ToString() + "~" + re;
        }
        //根据id获取某一用户信息
        public string GetUserListByID()
        {
            var re = "";
            var userid = int.Parse(Request["userid"]);
            UserBLL ubll = new UserBLL();
            var list = ubll.getUserListByID(userid);
            if (list != null)
            {
                re = JsonTools.ToJson(list);
            }
            return re;
        }
        //更新用户
        public string UserUpdate()
        {
            var re = "";
            var ir = 0;
            var op = Request["op"];
            int id = int.Parse(Request["id"]);
            UserBLL ubll = new UserBLL();
            if (op == "-1")//删除操作
            {
                ir = ubll.DeleteUser(id);
            }
            else//添加修改操作
            {
                var loginname = Request["loginname"];
                var loginpwd = Request["pwd"];
                var username = Request["username"];
                var leftmenuids = Request["leftmenuid"];
                var weixinids = Request["weixinid"];
                var menuids = Request["menuid"];
                var operateids = Request["operateid"];
                var ip = Request["ip"];
                var csid = string.IsNullOrEmpty(Request["csid"]) ? 0 : int.Parse(Request["csid"]);
                var isvalid = string.IsNullOrEmpty(Request["isvalid"]) ? 0 : int.Parse(Request["isvalid"]);
                UserEntity u = new UserEntity();
                u.id = id;
                u.loginName = loginname;
                u.loginpwd = loginpwd;
                u.Username = username;
                u.leftmenuids = leftmenuids.Trim(',');
                u.weixinids = weixinids.Trim(',');
                u.menuids = menuids.Trim(',');
                //u.operates = operateids.Trim(',');
                u.operates = operateids.Trim(';');
                u.IPaddress = ip;
                u.csid = csid;
                u.createtime = DateTime.Now;
                u.updatetime = DateTime.Now;
                u.remark = "";
                u.Ipnum = 0;
                u.isvalid = isvalid;
                ir = ubll.UpdateUser2(u);
            }
            if (ir > 0)
            {
                re = "1";
            }
            return re;
        }

        //查询左边菜单列表
        public string QueryLeftMenuList()
        {
            var strJson = "";
            LeftMemuBLL bll = new LeftMemuBLL();
            var list = bll.getListLiftMemuAll();
            if (list != null && list.Count > 0)
            {
                list = list.Where(p => p.hisid != 0).ToList();
                strJson = JsonTools.ToJson(list);
            }
            return strJson;
        }
        //查询平台列表
        public string QueryWeixXinList()
        {
            var strJson = "";
            weixinUserBLL bll = new weixinUserBLL();
            var list = bll.getWeixinUser1();
            if (list != null && list.Count > 0)
            {
                strJson = JsonTools.ToJson(list);
            }
            return strJson;
        }
        //查询微信菜单列表
        public string QueryMenuList()
        {
            var strJson = "";
            var list = MenuBLL.getMenuList();
            if (list != null && list.Count > 0)
            {
                strJson = JsonTools.ToJson(list);
            }
            return strJson;
        }
        //查询操作列表
        public string QueryOperateList()
        {
            var strJson = "";
            var list = MenuBLL.getOperateList();
            if (list != null && list.Count > 0)
            {
                strJson = JsonTools.ToJson(list);
            }
            return strJson;
        }
        /// <summary>
        /// 判断是否有1发布，2添加，3修改，4删除权限
        /// </summary>
        /// <param name="operateflag"></param>
        /// <returns></returns>
        public string IsCheckAccess(int operateflag, int leftmenuid)
        {
            var uid = 0;
            UserEntity us = LoginUserInfo.GetLoginUserCache();
            if (us != null)
                uid = us.id;
            var list = MenuBLL.getOperateList(uid, leftmenuid);
            if (list != null && list.Count > 0)
            {
                var obj = list.Where(p => p.operate_id.Contains(operateflag.ToString()) == true);
                if (obj.Count() > 0)
                {
                    return "1";
                }
            }
            return "";
        }
        public string IsCheckAccess(int operateflag1, int operateflag2, int leftmenuid)
        {
            var uid = 0;
            UserEntity us = LoginUserInfo.GetLoginUserCache();
            if (us != null)
                uid = us.id;
            var list = MenuBLL.getOperateList(uid, leftmenuid);
            if (list != null && list.Count > 0)
            {
                var obj = list.Where(p => p.operate_id.Contains(operateflag1.ToString()) == true || p.operate_id.Contains(operateflag2.ToString()) == true);
                if (obj.Count() > 0)
                {
                    return "1";
                }
            }
            return "";
        }
        public string DisplayOperate(int operateflag, int leftmenuid)
        {
            var re = IsCheckAccess(operateflag, leftmenuid);
            if (re == "1")
            {
                return "";
            }
            else
            {
                return "none";
            }
        }
        public string DisplayOperate(int operateflag1, int operateflag2, int leftmenuid)
        {
            var re = IsCheckAccess(operateflag1, operateflag2, leftmenuid);
            if (re == "1")
            {
                return "";
            }
            else
            {
                return "none";
            }
        }

        //查询客服列表
        public string QueryCustomServerList()
        {
            var strJson = "";
            var list = CustomAccountManagerBLL.getCustomAccountManagerList();
            if (list != null && list.Count > 0)
            {
                strJson = JsonTools.ToJson(list);
            }
            return strJson;
        }
    }
}
